﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CIIS.Common;
using DevExpress.XtraGrid.Views.Base;

namespace CIIS
{
    public partial class BmManage : BaseUserControl
    {
        public BmManage()
        {
            InitializeComponent();
            setLookUpdedit();
            LoadGrid();
            gridView1.FocusedRowChanged += entity_FocusedRowChanged;
        }

        private void setLookUpdedit()
        {
            Framework.QueryInfo info = new Framework.QueryInfo();
            info.CustomSQL = "select table_name,if((c.TABLE_COMMENT=''), table_name,c.TABLE_COMMENT) comment from information_schema.tables c";
            info.Where.Add("kk", " and table_schema='xzxx' and table_type='base table' and table_name like 'bm_%'");
            System.Data.DataTable table = dao.Dao.ExcuteDataSet(info).Tables[0];
            this.lookUpEdit1.Properties.DataSource = table;
            lookUpEdit1.Properties.Columns.Clear();
            lookUpEdit1.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo()
            { Caption="类型",
                 FieldName="comment"
            });
            lookUpEdit1.EditValue = table.Rows[0][0].ToString();

        }
        private void LoadGrid()
        {
            var obj = lookUpEdit1.EditValue.ToString();
            DevControlUtils.Instance.InitGridView(gridView1, false);
            gridControl1.DataSource = null;
            gridControl1.DataSource = DataUtils.Instance.ReloadDataList(obj);
            gridControl1.RefreshDataSource();
        }
        private object entity
        {
            get;
            set;
        }
        void entity_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle >= 0)
            {
                entity = this.gridView1.GetRow(e.FocusedRowHandle);
            }
            else
            {
                entity = null;

            }
        }
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            BMEidt edit = new BMEidt(lookUpEdit1.EditValue.ToString());
            if (edit.ShowDialog() == DialogResult.OK)
            {
                LoadGrid();
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (entity != null)
            {
                var o = entity as Framework.Domain.Entity;
                o.State.MarkDeleted();
                dao.Dao.SaveOrUpdate(entity);
                LoadGrid();
            }
            else
            {
                MessageBox.Show("请选择要删除的记录！");
            }
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            if (entity != null)
            {
                BMEidt edit = new BMEidt(entity);
                if (edit.ShowDialog() == DialogResult.OK)
                {
                    LoadGrid();
                }
            }
        }

        private void lookUpEdit1_EditValueChanged(object sender, EventArgs e)
        {
            LoadGrid();
        }
    }
}
